package com.example.tourstor.database;

import android.os.Bundle;
import android.util.Log;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

public class MerchantItem {

    private int merchantNum;
    private String merchantName;
    private String merchantPlace;
    private int merchantTel;
    private int merchantMobile;
    private String merchantClass;
    private String merchantTime;
    private double merchantScore;

    private DBManager dbManager;
    private static final String TAG = "MerchantItem";

    public MerchantItem(){
        dbManager = DBManager.createInstance();
    }


    public ArrayList<HashMap<String, String>> getMerchantInfo(String sql){
        Log.i(TAG, "getMerchantInfo: ");
        dbManager.connectDB();
        ArrayList<HashMap<String,String>> taskList=new ArrayList<HashMap<String, String>>();
        try {
            ResultSet rs = dbManager.stmt.executeQuery(sql);
            while(rs.next()){

                HashMap<String,String> post=new HashMap<String,String>();
                post.put("Mno",rs.getString("Mno"));
//                post.put("Mno",rs.getString("Mimg"));
                post.put("Mname",rs.getString("Mname"));
                post.put("Mplace",rs.getString("Mplace"));
                post.put("Mclass",rs.getString("Mclass"));
                post.put("Mscore",rs.getString("Mscore"));
                taskList.add(post);
                Log.i(TAG, "getMerchantInfo: Mno = " + rs.getString("Mno"));

            }
            rs.close();
            Log.i(TAG, "getMerchantInfo: Select successful");
        } catch (SQLException e) {
            Log.i(TAG, "getMerchantInfo: SQLException");
            e.printStackTrace();
        }
        dbManager.closeDB();
        return taskList;
    }


    public Bundle getMerchantItem(int n) {
        Log.i(TAG, "getMerchantItem: ");

        dbManager.connectDB();
        Bundle bundle = new Bundle();
        String sql = "select * from merchant where Mno = " + n;
        try {
            ResultSet rs = dbManager.stmt.executeQuery(sql);
            rs.next();
            bundle.putInt("Mno", rs.getInt("Mno"));
            bundle.putString("Mname", rs.getString("Mname"));
            bundle.putString("Mplace", rs.getString("Mplace"));
            bundle.putString("Mtel", rs.getString("Mtel"));
            bundle.putString("Mmobile", rs.getString("Mmobile"));
            bundle.putString("Mclass", rs.getString("Mclass"));
            bundle.putString("Mclosetime", rs.getString("Mclosetime"));
            bundle.putString("Mopentime", rs.getString("Mopentime"));
            bundle.putDouble("Mscore", rs.getDouble("Mscore"));
            Log.i(TAG, "getMerchantItem: Mno = " + rs.getInt("Mno"));

            dbManager.closeDB();
            return bundle;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }


    public int insertImg() {
        dbManager.connectDB();
        int ret = 0;
        try {
            String sql = "";
            ret = dbManager.stmt.executeUpdate(sql);
            Log.i(TAG, "insertImg: success");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        dbManager.closeDB();
        return ret;
    }




    public int update() {
        dbManager.connectDB();
        int ret = 0;
        try {
            String sql = "";
            ret = dbManager.stmt.executeUpdate(sql);
            Log.i(TAG, "update: sucess");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        dbManager.closeDB();
        return ret;
    }


    public int delete(int n) {
        dbManager.connectDB();
        int ret = 0;
        try {
            String sql = "delete from merchant where id = " + n;
            ret = dbManager.stmt.executeUpdate(sql);
            Log.i(TAG, "delete: sucess");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        dbManager.closeDB();
        return ret;
    }








}
